package org.i_plus.model.dao;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.i_plus.model.MapBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class MapDAOHibernate {

	private SessionFactory sessionFactory;

	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}

	public List<MapBean> selectAll() {

		Session session = this.sessionFactory.getCurrentSession();
		Query query = session.createQuery("from MapBean ");
		return query.list();
	}

	public MapBean selectM(int m_num) {
		Session session = this.sessionFactory.getCurrentSession();
		return (MapBean) session.get(MapBean.class, m_num);
	}

	public void insertM(String account, String m_content, double x_axis,
			double y_axis, String m_title, byte[] m_pic) {
		Session session = this.sessionFactory.getCurrentSession();
		// Transaction tx = session.beginTransaction();
		// System.out.println("tx begin");
		MapBean bean = new MapBean();
		// System.out.println("bean create");
		bean.setAccount(account);
		bean.setM_content(m_content);
		bean.setX_axis(x_axis);
		bean.setY_axis(y_axis);
		bean.setM_title(m_title);
		bean.setM_pic(m_pic);
		// System.out.println("bean set already");
		session.save(bean);
		// tx.commit();
		// System.out.println("tx commit");
		// session.close();
	}

	public void updateM(String account, int m_num, String m_content,
			double x_axis, double y_axis, String m_title, byte[] m_pic) {
		Session session = this.sessionFactory.getCurrentSession();
		// Transaction tx = session.beginTransaction();
		MapBean bean = new MapBean();
		bean.setM_num(m_num);
		bean.setAccount(account);
		bean.setM_content(m_content);
		bean.setX_axis(x_axis);
		bean.setY_axis(y_axis);
		bean.setM_title(m_title);
		bean.setM_pic(m_pic);
		session.update(bean);

		// tx.commit();
		// session.close();

	}

	public void deleteM(int m_num) {
		Session session = this.sessionFactory.getCurrentSession();
		// Transaction tx = session.beginTransaction();
		MapBean bean = (MapBean) session.get(MapBean.class, m_num);
		session.delete(bean);
		// tx.commit();
		// session.close();
	}

	public static void main(String[] args) {

		ApplicationContext context = new ClassPathXmlApplicationContext(
				"beans.config.xml");
		SessionFactory sessionFactory = (SessionFactory) context
				.getBean("sessionFactory");

		Transaction tx = sessionFactory.getCurrentSession().beginTransaction();

		MapDAOHibernate mdao = (MapDAOHibernate) context.getBean("mapDAO");

		// mdao.insertM("BB", "glkji", 1.11, 4.12, "test3", null);
		mdao.selectM(11);
		// mdao.selectAll();
		// mdao.updateM("BB",5 ,"gi11", 1.21, 1.13, "test1", null);
		// mdao.deleteM(5);
		tx.commit();

		// MapDAOHibernate map = new MapDAOHibernate();
		// map.insertM("456", "def", 3.33, 4.44, "test2", null);
		// map.updateM("123", 4, "ghi", 1.11, 5.55, "test3", null);
		// map.deleteM(5);

		System.out.println("done");
	}
}
